<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/6
 * Time: 20:55
 */


require_once "../../php/config/database.php";
require_once "userDb.php";
$UserDb=new UserDb();
$user=$_POST['user'];
$class=$_POST['className'];
$code=$_POST['code'];
$email=$_POST['email'];
$pass=$_POST['pass'];
$pass=md5($pass);
$addtime=time();

$dataAA=$UserDb->queryUser($user);
try{
    $res = $dataAA;
    if(!$res){

        $data=$UserDb->selectClassName($class);
        try {
            $result = $data;
            if ($result) {
                $dataA=$UserDb->selectAdminCode($code);
                $admin=$data[0]['user'];
                try{
                    $resultA = $dataA;
                    if ($resultA) {
                        $dataB=$UserDb->addUser($user,$pass,$class,$code,$email,$admin,$addtime);
                        try{
                            $resultB = $dataB;
                            if($resultB){
                                $dat = array(
                                    'message' =>"恭喜你，注册成功,",
                                );
                                echo json_encode($dat);
                            }
                            else{
                                $dat = array(
                                    'message' =>"对不起，注册失败,",
                                );
                                echo json_encode($dat);

                            }

                        } catch (Exception $e) {
                            echo $e->getMessage();}

                    }
                    else{
                        $dat = array(
                            'message' =>"注册失败,您的邀请码错误，请核对后重试",
                        );
                        echo json_encode($dat);
                    }


                }

                catch (Exception $e) {

                    // 回滚事务
                    // $pdo->rollback();
                    echo $e->getMessage();}


            }
            else {
                $dat = array(

                    'message' =>"注册失败,您的团队名称错误",

                );
                echo json_encode($dat);

            }
        }
        catch (Exception $e) {

            // 回滚事务
            // $pdo->rollback();
            echo $e->getMessage();}

    }

    else{
        $dat = array(
            'message' =>"该用户已存在",
        );
        echo json_encode($dat);

    }

}    catch (Exception $e) {

    // 回滚事务
    // $pdo->rollback();
    echo $e->getMessage();}



